<?php

namespace backend\modules\goods\controllers;

use backend\extensions\ApiBaseController;
use backend\services\goods\GoodsCateService;

class CateController extends ApiBaseController
{
    /**
     * 列表
     * * */
    public function actionList()
    {
        $params = \Yii::$app->request->get();
        $rules = [
            [[], 'default', 'value' => '', 'message' => '名称检索'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = GoodsCateService::getList($this->params);
        return $this->jsonSuccess($data);
    }

    /**
     * 新增
     * * */
    public function actionSave()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name', 'sort', 'state'], 'required', 'message' => '{attribute}属必填项'],
            [['id', 'img', 'pid', 'url'], 'default', 'value' => '', 'message' => '非必填参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if ($this->params['id']) {
            return GoodsCateService::update($this->params);
        }
        unset($this->params['id']);
        return GoodsCateService::create($this->params);
    }

    /**
     * 用户详情
     * * */
    public function actionDetail()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return GoodsCateService::detail($this->params);
    }

    /**
     * 删除用户
     * * */
    public function actionDel()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return GoodsCateService::delete($this->params);
    }
}
